﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;


namespace DatabaseSynchronizer {

    public partial class SelectServer : Form {

        const string InvalidURL = "Invalid server name/URL.";
        const string InvalidPort = "Invalid port number";
        const string ServerNameEmpty = "Server name cannot be empty.";

        public static string Username = string.Empty;
        public static string Password = string.Empty;
        public static bool RememberCredentials = false;

        public SelectServer() {

            InitializeComponent();
        }

        private void ServerNameTextBox_TextChanged(object sender, EventArgs e) {

            if (ServerNameTextBox.Text.Trim() == string.Empty) {
                PreviewTextBox.Text = ServerNameEmpty;
            }
            else if (!Utils.IsAlphanumeric(ServerNameTextBox.Text)) {
                PreviewTextBox.Text = InvalidURL;
            }
            else {
                SetPreview();
            }
        }

        private void PathTextBox_TextChanged(object sender, EventArgs e) {
            SetPreview();
        }

        private void PortNumTextBox_TextChanged(object sender, EventArgs e) {

            if (Utils.IsValidPortNumber(PortNumTextBox.Text)) {
                SetPreview();
            }
            else {
                PreviewTextBox.Text = InvalidPort;
            }
        }

        private void HTTPRadioButton_CheckedChanged(object sender, EventArgs e) {

            HTTPSRadioButton.Checked = (!HTTPRadioButton.Checked);
            ProtocolRadioButtonCheckedChanged();
            SetPreview();
        }

        private void HTTPSRadioButton_CheckedChanged(object sender, EventArgs e) {

            HTTPRadioButton.Checked = (!HTTPSRadioButton.Checked);
            ProtocolRadioButtonCheckedChanged();
            SetPreview();
        }

        private void ProtocolRadioButtonCheckedChanged() {
            
            if (PortNumTextBox.Text == "8080" && HTTPSRadioButton.Checked) {
                PortNumTextBox.Text = "443";
            }
            else if (PortNumTextBox.Text == "443" && HTTPRadioButton.Checked) {
                PortNumTextBox.Text = "8080";
            }
        }

        private void SetPreview() {

            ErrorLabel.Text = string.Empty;

            if (IsAValidServer()) {
                string serverName = "http{0}://{1}{2}/{3}";

                string parm0 = string.Empty;
                string parm1 = string.Empty;
                string parm2 = string.Empty;
                string parm3 = string.Empty;

                if (HTTPSRadioButton.Checked) {
                    parm0 = "s";
                }

                parm1 = ServerNameTextBox.Text.Trim();

                if (!(HTTPSRadioButton.Checked && PortNumTextBox.Text.Trim() == "443")) {
                    parm2 = ":" + PortNumTextBox.Text.Trim();
                }

                parm3 = PathTextBox.Text.Trim();

                PreviewTextBox.Text = string.Format(serverName, parm0, parm1, parm2, parm3);
                OkButton.Enabled = true;
            }
            else {
                OkButton.Enabled = false;
            }
        }

        private bool IsAValidServer() {

            bool valid = true;

            if (ServerNameTextBox.Text.Trim() == string.Empty) {
                PreviewTextBox.Text = ServerNameEmpty;
                valid = false;
            }

            if (!Utils.IsAlphanumeric(ServerNameTextBox.Text)) {
                PreviewTextBox.Text = InvalidURL;
                valid = false;
            }
            else if (!Utils.IsValidPortNumber(PortNumTextBox.Text)) {
                PreviewTextBox.Text = InvalidPort;
                valid = false;
            }

            return valid;
        }

        private void CancelButton_Click(object sender, EventArgs e) {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void OkButton_Click(object sender, EventArgs e) {

            string errorMessage = string.Empty;

            switch (Utils.IsValidTfsUrl(PreviewTextBox.Text, null, out errorMessage)) {

                case ConnectionResult.Success:
                    SettingsForm.TFSServerURL = PreviewTextBox.Text;
                    DialogResult = DialogResult.OK;
                    Close();
                    break;

                case ConnectionResult.InvalidCredentials:
                    Form credentials = new CredentialForm(new string[] { PreviewTextBox.Text });
                    DialogResult dialogResult = credentials.ShowDialog();
                    if (dialogResult == DialogResult.OK) {
                        SettingsForm.TFSServerURL = PreviewTextBox.Text;
                        DialogResult = DialogResult.OK;
                        Close();
                    }
                    else {
                        DialogResult = DialogResult.Cancel;
                        Close();
                    }
                    
                    break;
                
                case ConnectionResult.Failure:
                    ErrorLabel.Text = errorMessage;

                    break;
                
            }
        }
    }
}